import BackGround from './runtime/background'
import GameInfo   from './runtime/gameinfo'
import Music      from './runtime/music'

let ctx   = canvas.getContext('2d')
let databus = new oni.DataBus()
const screenWidth = window.innerWidth
const screenHeight = window.innerHeight
/**
 * 游戏主函数
 */
export default class Main {
  constructor() {
    this.restart()
    //游戏启动
    this.run()
  }
  run(){
    window.requestAnimationFrame(
      this.loop.bind(this),
      canvas
    )
  }
  restart() {
    databus.reset()
    canvas.removeEventListener(
      'touchstart',
      this.touchHandler
    )
    this.bg       = new BackGround(ctx)
    this.gameinfo = new GameInfo()
    this.music    = new Music()
  }

  /**
   * 随着帧数变化的敌机生成逻辑
   * 帧数取模定义成生成的频率
   */
  enemyGenerate() {
    if ( databus.frame % 30 === 0 ) {
      // let enemy = databus.pool.getItemByClass('enemy', Enemy)
      // enemy.init(6)
      // databus.enemys.push(enemy)
    }
  }

  // 全局碰撞检测
  collisionDetection() {
    let that = this

    // databus.bullets.forEach((bullet) => {
    //   for ( let i = 0, il = databus.enemys.length; i < il;i++ ) {
    //     let enemy = databus.enemys[i]

    //     if ( !enemy.isPlaying && enemy.isCollideWith(bullet) ) {
    //       enemy.playAnimation()
    //       that.music.playExplosion()

    //       bullet.visible = false
    //       databus.score  += 1

    //       break
    //     }
    //   }
    // })

    // for ( let i = 0, il = databus.enemys.length; i < il;i++ ) {
    //   let enemy = databus.enemys[i]

    //   if ( this.player.isCollideWith(enemy) ) {
    //     databus.gameOver = true

    //     break
    //   }
    // }
  }

  //游戏结束后的触摸事件处理逻辑
  touchEventHandler(e) {
     e.preventDefault()

    let x = e.touches[0].clientX
    let y = e.touches[0].clientY

    let area = this.gameinfo.btnArea

    if (   x >= area.startX
        && x <= area.endX
        && y >= area.startY
        && y <= area.endY  )
      this.restart()
    }

    /**
     * canvas重绘函数
     * 每一帧重新绘制所有的需要展示的元素
     */
    render() {
    ctx.clearRect(0, 0, canvas.width, canvas.height)
    databus.ui
           .forEach((item) => {
              item.drawToCanvas(ctx)
            })
    this.gameinfo.renderGameScore(ctx, databus.score)
  }

  // 游戏逻辑更新主函数
  update() {
    this.enemyGenerate()
    this.collisionDetection()
  }

  // 实现游戏帧循环
  loop() {
    databus.frame++
    this.update()
    this.render()
    if ( databus.frame % 20 === 0 ) {
      //每20 执行一次
    }

    // 游戏结束停止帧循环
    if ( databus.gameOver ) {
      this.gameinfo.renderGameOver(ctx, databus.score)

      this.touchHandler = this.touchEventHandler.bind(this)
      canvas.addEventListener('touchstart', this.touchHandler)

      return
    }

    window.requestAnimationFrame(
      this.loop.bind(this),
      canvas
    )
  }
}
